package com.briup.day22;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Test_Read {
    /** 注释内容:可访问的字符有几个  读取一次可读取的字节个数就减少一个 skip 跳一个字节*/
    public static void main(String[] args) throws Exception {
        InputStream is = new FileInputStream("D:/Test/a.txt");
//        System.out.println(is.available());//可获得三个
//        int r=is.read();
//        System.out.println(r);
//        System.out.println(is.available());
//        is.close();
//    }
        int r = is.read();
        System.out.println(r);
        is.skip(1);//跳过n个字节
        r = is.read();
        System.out.println(r);
    }
    /** read(byte[] arr,int off,int len)*/
    public static void main03(String[] args) throws Exception {
        InputStream is = new FileInputStream("D:/Test/a.txt");
        byte[] arr = new byte[10];
        int len = is.read(arr,3,5);
        System.out.println(len);
        System.out.println("读取到的有效数目:" + len);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        is.close();
    }
    public static void main02(String[] args) throws IOException {
        /*
          read方法逐字节读取  将读取内容放入小数组 byte
         */
        InputStream is=new FileInputStream("D:/Test/a.txt");
        byte[] arr=new byte[10];
        int len= is.read(arr);
        System.out.println("读取到有效数目: "+len);
        for (int i=0;i<len;i++) {
            System.out.println(arr[i]);
        }
        int r = is.read();
        System.out.println(r);//-1
        is.close();
    }
    /**
     * read方法逐字节读取 */
    public static void main01(String[] args) throws IOException {
        //1.创建流对象
        InputStream is = new FileInputStream("D:/test/a.txt");
        //2.调用InputStream中的方法逐字节读取
        int r = is.read();
        System.out.println("第一次读取"+r);
        r=is.read();
        System.out.println("第二次读取"+r);
        r=is.read();
        System.out.println("第三次读取"+r);
        r=is.read();//读取到文件末尾再读返回-1
        System.out.println("第四次读取"+r);
        //3.释放资源 关闭流
        is.close();
    }
}
